clear
set more off, perm
cd /Users/zachbrown/Projects/PriceTransparency/Data/
sysdir set PLUS /Users/zachbrown/Projects/PriceTransparency/Code/ado


// Use medical claims
global year_min 2005
global year_max 2014 
global keep_vars "member_key year from_date amt_copay amt_coins amt_deduct claim_status_orig"


	forval yr = $year_min(1)$year_max {
		disp "year: `yr'"
		tempfile tmpdata
		shell nice gunzip Raw/med_clm/med_clm_`yr'_clean.dta -c > `tmpdata'
		append using `tmpdata', keep($keep_vars)
	}


gen fyear=year(from_date)
egen memberyr_tag = tag(member_key fyear)
tab memberyr_tag, miss

// Drop denied claims
drop if claim_status_orig==4


// Calculate total annual deducatible
bys member_key fyear: egen tot_deduct = sum(amt_deduct)
gen has_deduct = (tot_deduct>2) & tot_deduct~=.

tab has_deduct, miss
tab has_deduct if memberyr_tag, miss
drop tot_deduct


// Find date in which individual is past deductible
sort member_key fyear from_date
gen deduct_date = from_date if amt_deduct>0
replace deduct_date=0 if deduct_date==.
bys member_key fyear: egen last_deduct = max(deduct_date)
drop deduct_date
replace last_deduct=. if has_deduct==0
format last_deduct %d
gen past_deduct = (from_date>last_deduct) if has_deduct==1
drop last_deduct


// Calculate total annual copay
bys member_key fyear: egen tot_copay = sum(amt_copay)
gen has_copay = (tot_copay>0) & tot_copay~=.

tab has_copay, miss
tab has_copay if memberyr_tag, miss // one observation per individual-year
drop tot_copay


// Calculate total annual coinsurance
bys member_key fyear: egen tot_coin = sum(amt_coin)
gen has_coin = (tot_coin>0) & tot_coin~=.
drop tot_coin


contract member_key from_date has_deduct past_deduct has_copay has_coin, freq(n)
tab n
drop n
duplicates report member_key from_date
duplicates drop member_key from_date, force // What are these?
compress
gsave build/deductible_info.dta, replace

// Collapse to month level
gen ym = ym(year(from_date),month(from_date))
format ym %tm
collapse (max) has_deduct has_copay has_coin, by(member_key ym)

gsave build/deductible_info_ym.dta, replace




